using System;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;


namespace EntLibContrib.PolicyInjection
{
    [Assembler(typeof(NotMatchingRuleAssembler))]
    public class NotMatchingRuleData : CompositeMatchingRuleData
    {
        private const string NegatedRulePropertyName = "NegatedRule";

        public NotMatchingRuleData()
        {
        }

        public NotMatchingRuleData(string name)
            : base(name, typeof(NotMatchingRule))
        {
        }
    }

    internal class NotMatchingRuleAssembler : IAssembler<IMatchingRule, MatchingRuleData>
    {
        public IMatchingRule Assemble(IBuilderContext context, MatchingRuleData objectConfiguration,
            IConfigurationSource configurationSource,
            ConfigurationReflectionCache reflectionCache)
        {
            NotMatchingRuleData ruleData = (NotMatchingRuleData)objectConfiguration;
            if (ruleData.MatchingRules.Count != 1)
            {
                throw new ConfigurationErrorsException("NotMatchingRule should contain extactly 1 child rule");
            }
            IMatchingRule negated = MatchingRuleCustomFactory.Instance.Create(context, ruleData.MatchingRules.Get(0), configurationSource, reflectionCache);
            return new NotMatchingRule(negated);
        }
    }
}
